package com.cencat.framework.core.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.cencat.framework.core.util.TenantContextHolder;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MultiTenantConfig {
    
    @Bean
    public TenantLineHandler tenantLineHandler() {
        return new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                Long tenantId = TenantContextHolder.getCurrentTenantId();
                return new LongValue(tenantId != null ? tenantId : 0L);
            }
            
            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }
            
            @Override
            public boolean ignoreTable(String tableName) {
                return tableName.startsWith("sys_");
            }
        };
    }
}